package com.work.libtest2.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.work.libtest2.dto.RewardAndPunishmentDTO;
import com.work.libtest2.entity.R;
import com.work.libtest2.entity.RewardAndPunishment;
import com.work.libtest2.service.RewardAndPunishmentService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;

/**
 * ClassName: RewardAndPunlishment
 * Package: com.work.libtest2.controller
 * Description:
 *
 * @Author
 * @Create 2024/6/10 15:40
 * @Version 1.0
 */
@RestController
@Slf4j
@RequestMapping("/rewardAndPunishment")
public class RewardAndPunishmentController {

    @Resource
    private RewardAndPunishmentService rewardAndPunishmentService;

    @GetMapping("{currenPage}/{pageSize}")
    public R getPage(@PathVariable Integer currenPage, @PathVariable Integer pageSize, RewardAndPunishmentDTO rewardAndPunishmentDTO){
        IPage<com.work.libtest2.entity.RewardAndPunishment> page = rewardAndPunishmentService.getPage(currenPage,pageSize,rewardAndPunishmentDTO);
        if (currenPage>page.getPages()){
            page = rewardAndPunishmentService.getPage((int)page.getPages(),pageSize,rewardAndPunishmentDTO);
        }
        return new R(true,page);
    }

    /**
     * 添加奖励记录
     * */
    @PostMapping("/addReward")
    public R addReward(@RequestBody RewardAndPunishment rewardAndPunishment){
        int i =rewardAndPunishmentService.addReward(rewardAndPunishment);
        return new R(i>0,i>0?"添加成功":"添加失败");
    }
    /**
     * 删除
     * */
    @DeleteMapping("{rapId}")
    public R delete(@PathVariable Integer rapId){
       int i =  rewardAndPunishmentService.deleteById(rapId);
       return new R(i>0);
    }
    /**
     * 查找
     * */

    @GetMapping("{rapId}")
    public R getById(@PathVariable Integer rapId){
        RewardAndPunishment rewardAndPunishment = rewardAndPunishmentService.getById(rapId);
        return new R(true,rewardAndPunishment);
    }
    /**
     * 修改
     * */
    @PutMapping
    public R updateById(@RequestBody RewardAndPunishment rewardAndPunishment){
        int i = rewardAndPunishmentService.saveById(rewardAndPunishment);
        return new R(i>0);
    }
}
